<?php

namespace App\Http\Controllers\admin\product;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Product;
use App\Models\Type;
use App\Models\Image;
use DB;
use Illuminate\Support\Facades\Session;

use Intervention\Image\ImageManagerStatic as Img;
class ProductController extends Controller
{
    /**
     ***商品列表页***
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //$list = DB::table('product')->get();
        //dd($list);
        $product = new Product();
        $list = $product->proGet();
        return view('admin.product.product',['list'=>$list]);
    }


    /**
     ***绑定商品添加页面****
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('admin.product.productAdd');
    }

    /**
     * ***执行添加的方法****
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|null
     */
    public function store(Request $request)
    {
        //dd(Session::get('storelist')['id']);
        $product = new Product();

        $data = $request->except('_token','select','label','file','type');
        $select = $request->input('select');
        $label = $request->input('label');
        //dd($request->file('file')[0]);

        $data['category_path'] = '';
        $data['label'] = '';
        $data['store_id'] = Session::get('storelist')['id'];
        foreach ($select as $v){
            $data['category_path'] .= $v.',';
        }
        $tid = $request->input('type');
        //把type顶级分类加入标签
        $data['label'] = Type::find($tid)->name.',';
//        foreach ($label as $v){
//            $data['label'] .= $v.',';
//        }

        $insert = $product->create($data);

        $bool = $insert->save();
        //dd($bool);
        $owner_id = $insert->id;

        $imgs = $product->photos($request);
        //dd(imgs);
        foreach ($imgs as $v){
            $dataImg['owner_id'] = $owner_id;
            $dataImg['name'] = $v;
            $insertImg = Image::create($dataImg);

           $insertImg->save();
        }
        $photo = array_shift($imgs);
        $img = Img::make('./upload/product/'.$photo)->resize(200,200);
        $img->save('./upload/product/s_'.$photo);
        $product->where('id',$owner_id)->update(['preview'=>('s_'.$photo)]);

        if($bool){
            return redirect('/store/product');
        }else{
            return null;
        }
    }

    /**
     ****用ajax取得所有前台板块***
     * @param Request $request
     * @return mixed
     */
    public function type(Request $request)
    {

        $type_id = $request->input('type_id');

        $list = DB::table('type')->where('type_id',$type_id)->get();


        return  $list;
    }

    /**
     ***用ajax取得所有药品的分类***
     * @param Request $request
     * @return mixed
     */
    public function categroy(Request $request)
    {

        $pid = $request->input('pid');

        $list = DB::table('category')->where('pid',$pid)->where('store_id',session('storelist')['id'])->get();


        return  $list;
    }

    /**
     ***查看商品详情***
     * @param Request $request
     * @return mixed
     */
    public function detail(Request $request)
    {
        $id = $request->input('id');
        $data = Product::find($id,['name','specifications','summary','instructions']);
        return $data;
    }


    /**
     ****ajax查看商品图片****
     * @param Request $request
     * @return string
     */
    public function proImage(Request $request)
    {
        $owner_id = $request->input('mid');

        $image = new Image();

        $data = $image->imgFind($owner_id);

        return json_encode($data);

    }

    /**
     ***更改预览图***
     * @param Request $request
     * @return string
     */
    public function preImg(Request $request)
    {
        $id = $request->input('id');

        $mid = $request->input('mid');

        $image = new Image();

        $imgname = $image->find($id)->name;


        $img = Img::make('./upload/product/'.$imgname)->resize(200,200);
        $picname = 'h_'.rand(0,999).$imgname;
        $img->save('./upload/product/'.$picname);

        $product = Product::find($mid);
        if(!empty($product->preview) && file_exists('./upload/product/'.$product->preview)){
            unlink('./upload/product/'.$product->preview);

        }

        $product->preview = $picname;

        $bool = $product->save();

        if($bool){
            $imgUrl = asset('upload/product').'/'.$picname;

            return json_encode($imgUrl);
        }


    }

    /**
     * **添加轮播图**
     * @param Request $request
     */
    public function addImg(Request $request)
    {
        //dd($request->file());
        $product = new Product();
        $owner_id = $request->input('id');

        $imgs = $product->photos($request);
        //dd($imgs);
        foreach ($imgs as $v){
            $dataImg['owner_id'] = $owner_id;
            $dataImg['name'] = $v;
            $insertImg = Image::create($dataImg);

            $bool = $insertImg->save();
        }
        dd($bool);



    }

    /**
     * **删除图片***
     * @param Request $request
     * @return int|null
     */
    public function delImg(Request $request)
    {
        $id = $request->input('id');

        $image = Image::find($id);
        $picname = $image->name;

        $bool = unlink('./upload/product/'.$picname);
        if($bool){
            $row = $image->delete($id);
        }

        if($row){

            return 1;
        }else{
            return null;
        }
    }

    /**
     * **更改商品***
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $product = new Product();

        $list = $product->proEdit($id);

        return view('admin.product.productEdit',['list'=>$list]);

    }

    /**
     ****改变状态****
     * @param Request $request
     * @return int
     */
    public function status(Request $request)
    {
        $id = $request->input('id');

        $data = Product::find($id);

        $data->status = $data->status==1?2:1;

        $bool = $data->save();

        if($bool){
            return 1;
        }else{
            return 0;
        }
    }

    public function hod(Request $request)
    {
        $id = $request->input('id');

        $data = Product::find($id);

        $data->hod = $data->hod==1?2:1;

        $bool = $data->save();

        if($bool){
            return 1;
        }else{
            return 0;
        }
    }

    public function update($id,Request $request)
    {
        $product = new Product();

        $data = $request->except('_token','select','label','type','_method');
        //dd($data);

        $select = $request->input('select');
        if(!empty($select)){
            $data['category_path'] = '';
            foreach ($select as $v){
                $data['category_path'] .= $v.',';
            }
        }

        $label = $request->input('label');
        if (!empty($label)){
            $data['label'] = '';
            $tid = $request->input('type');
            $data['label'] = Type::find($tid)->name.',';
            foreach ($label as $v){
                $data['label'] .= $v.',';
            }
        }


         $bool = $product->where('id',$id)->update($data);
        //dd($bool);


        if($bool){
            return redirect('/store/product');
        }else{
            return null;
        }
    }


}
